import java.util.Arrays;
public class Array {

    //二维数组
    public static void main(String[] args) {
        /*三种定义二维数组方式
        int[][] array1 = new int[2][3];
        int[][] array2 = new int[][]{{1,2,3},{4,5,6}};
        int[][] array3 = {{1,2,3},{4,5,6}};
        //不指定列数，会置空
         */
        int[][] array = {{1,2,3},{4,5,6}};
        System.out.println(Arrays.deepToString(array));//得到每一位
        for (int[] arr:array) {
            for (int x: arr) {
                System.out.println(x+" ");
            }
            System.out.println();
        }
    }

    //数组拷贝
    public static void main4(String[] args) {
        int[] array1 = {1,23,4,6,5,78};
        int[] array = array1.clone();//直接克隆
        int[] array2 = Arrays.copyOf(array1,array1.length);
        int[] array3 = Arrays.copyOf(array1,2*array1.length);//扩容为两倍
        //范围拷贝
        int[] array4 = Arrays.copyOfRange(array1,2,5);//[2,5) - 下标区间
    }
    //模拟实现c中的strcpy - 字符串拷贝
    public static void main3(String[] args) {
        int[] array1 = {1,4,6,7,16,9,2,31};
        int[] array2 = new int[array1.length];
        for (int i = 0; i < array1.length; i++) {
            array2[i] = array1[i];
        }
    }
    //实现tostring
    public static String mytostring(int[] tmp){
        if(tmp == null){
            return "null";
        }
        String ret= "{";
        for (int i = 0; i < tmp.length; i++) {
            ret += tmp[i];
            if(i != tmp.length-1){
                ret += "'";
            }
        }
        ret += "]";
        return ret;
    }
    public static void main2(String[] args) {
        int[] arr1 = {1,2,3,4};
        System.out.println(arr1);
        int[] arr2 = arr1;
        arr2[1] = 99;
        System.out.println(arr1);
        System.out.println(arr2);
    }
    public static void main1(String[] args) {
        //得到数组每一位
        int[] array = {1,2,3};
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
        //数组中数据的类型定义的变量 : 数组名
        //for each 增强for循环
        for (int x: array) {
            System.out.print(x+" ");
        }
        //Arrays - 把数组内容转化为字符串
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }
    public static void main0(String[] args) {
        int[] array = {1,2,3};//直接赋值  静态初始化
        int[] array2 = new int[]{1,2,3};//动态初始化
        int[] array3 = new int[10];//只分配内存，不进行初始化
        int len = array.length;
    }
}
